package components;

import entity.DatabaseTest;
import entity.Test;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class TestsInputTable extends JTable {
    
    private DatabaseTest[] databaseTests;

    public TestsInputTable() {
    }

    @Override
    public TableCellEditor getCellEditor(int row, int column) {
        if (column == 1 && !databaseTests[row].hasRush()) {
            return null;
        }
        return super.getCellEditor(row, column);
    }

    @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
        if (column == 1 && !databaseTests[row].hasRush()) {
            return new EmptyCellRenderer();
        }
        return super.getCellRenderer(row, column);
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        boolean normalSelected = (boolean) getValueAt(row, 0);
        boolean rushSelected = (boolean) getValueAt(row, 1);
        
        if (normalSelected) {
            super.setValueAt(false, row, 0);
        }
        
        if (rushSelected) {
            super.setValueAt(false, row, 1);
        }
        
        super.setValueAt(aValue, row, column);
    }

    public void setTests(List databaseTests) {
        this.databaseTests = new DatabaseTest[databaseTests.size()];
        for (int i = 0; i < databaseTests.size(); i++) {
            DatabaseTest databaseTest = (DatabaseTest)databaseTests.get(i);
            this.databaseTests[i] = databaseTest;
        }
        DefaultTableModel model = (DefaultTableModel) getModel();
        model.setRowCount(0);
        
        for (int i = 0; i < this.databaseTests.length; i++) {
            DatabaseTest dbTest = this.databaseTests[i];
            model.addRow(new Object[] {false, false, dbTest.getName()});
        }
    }
    
    public void setTestsForDisplay(List tests) {
        DefaultTableModel model = (DefaultTableModel) getModel();
        model.setRowCount(0);
        databaseTests = new DatabaseTest[tests.size()];
        for (int i = 0; i < tests.size(); i++) {
            Test test = (Test)tests.get(i);
            databaseTests[i] = test.getDatabaseTest();
            if (test.isRush()) {
                model.addRow(new Object[] {false, true, databaseTests[i].getName()});
            } else {
                model.addRow(new Object[] {true, false, databaseTests[i].getName()});
            }
        }
        setEnabled(false);
    }
    
    public List getSelectedTests () {
        List selectedTests = new ArrayList();
        DefaultTableModel model = (DefaultTableModel) getModel();
        
        for (int i = 0; i < model.getRowCount(); i++) {
            Test test;
            
            if ((boolean) model.getValueAt(i, 0)) {
                test = new Test(databaseTests[i]);
                test.setIsRush(false);
                selectedTests.add(test);
            } else if ((boolean) model.getValueAt(i, 1)) {
                test = new Test(databaseTests[i]);
                test.setIsRush(true);
                selectedTests.add(test);
            }
        }
        
        return selectedTests;
    }
    
    class EmptyCellRenderer extends DefaultTableCellRenderer {

        @Override
        protected void setValue(Object value) {
            super.setValue("");
        }
        
    }
}
